package util;

import java.io.File;

import play.Play;

public class FileUtil {

	public static final String PicPath = "/public/uploads/";

	private static String getCurrName(String fileName) {

		if (null == fileName || fileName.length() == 0) {
			return "";
		}
		int lastIndexOf = fileName.lastIndexOf(".");
		if (lastIndexOf == -1) {
			return null;
		}
		String suffix = fileName.substring(lastIndexOf, fileName.length());
		String temp = fileName.substring(0, lastIndexOf);
		String prefix = String.valueOf(System.currentTimeMillis()) + temp;

		return prefix + suffix;
	}

	
	/**
	 * 更名文件并上传到指定文件夹。
	 * @param target
	 * @return
	 */
	public static String uploadFile(File target){
		
		File dest = new File(target.getParent()+ FileUtil.getCurrName(target.getName()));
		boolean renameTo = target.renameTo(dest);
		File to = new File(Play.applicationPath.getAbsoluteFile()+FileUtil.PicPath+ dest.getName());
		if (renameTo) {
			if (!to.exists()) {
				to.getParentFile().mkdirs();
			}
			play.libs.Files.copy(dest, to);
		}
		return FileUtil.PicPath+to.getName();
	}
	
	
	

	public static void main(String[] args) {

	}

}
